In p5 we use code to draw graphics on a canvas:
The coordinate system is different...
Adjust your robot's coordinates:
##Rectangles:
##Ellipses:
Draw a robot using:
We need to practice giving instructions. Coding is all about giving a computer instructions that it will understand.
List the shapes and write down their information
function setup() { createCanvas(400, 400); }
function draw() { background(220); }
While setup() and draw() are defined by the developer in each sketch, most of the p5 functions we'll use today are defined by the p5 library, and invoked or “called” in our code.
setup()
draw()
When a function is invoked, or called, the programmer is asking the program to run the code within the function. The parenthesis operator is used to invoke the function.
rect(50, 50, 100, 100);
The values inside of the parentheses are known as arguments. These are used to change the outcome of a function.
function(argument1, argument2);
In Javascript, you can call a function by writing the name of the function followed by parentheses and a semicolon.
Any arguments are written in the parentheses with commas in between them.
createCanvas(600, 240);
This function creates an HTML canvas element that is 600 pixels wide and 240 pixels high.
The canvas is an HTML element that draws graphics and animations using scripting.
p5.Renderer2D.prototype.line = function(x1, y1, x2, y2) { var ctx = this.drawingContext; if (!this._doStroke) { return this; } else if (this._getStroke() === styleEmpty) { return this; } // Translate the line by (0.5, 0.5) to draw it crisp if (ctx.lineWidth % 2 === 1) { ctx.translate(0.5, 0.5); } ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke(); if (ctx.lineWidth % 2 === 1) { ctx.translate(-0.5, -0.5); } return this; };
line(x1,y1,x2,y2);
background(0);
This function gives our canvas a background color between 0 (black) and 255 (white).
p5.js makes drawing on the canvas easy by providing functions for us to draw shapes, but we need to be able to tell the program where to draw those shapes.
To do that, we need to understand the coordinate system that we’ll be drawing in.
Lines of code preceded by two slashes // will be read as comments in p5.js and will be ignored by the interpreter. The interpreter is the program that executes the instructions written in a programming language.
// move the following rectangle on the x axis rect(200, 50, 50, 50);
The function to draw a point is point(). The arguments are (x,y) which are the x and y coordinates.
point()
(x,y)
point(x,y);
The line() function requires four parameters: x1, y1, x2, and y2. For this function to work, it needs to know:
line()
The x and y coordinates of the first point
The x and y coordinates of the second point
The rect() function requires four parameters: x, y, width, and height. For this function to work, it needs to know:
rect()
All these parameters are measured in pixels.
The ellipse() function requires four parameters: x, y, width, and height. For this function to work, it needs to know:
ellipse()
In p5, shape and color functions are rendered to the canvas in the order they're written in the program from top to bottom.
Example - Remove the comments to see how the rectangles layer on one another.
triangle()
The triangle() function requires four parameters: x1, y1, x2, y2, x3 and y3 which are the three coordinates of the points on the triangle
quad()
The quad() function requires four parameters: x1, y1, x2, y2, x3, y3, x4, and y4 which are the three coordinates of the points on the quadrilateral.
To draw a polygon, first call the beginShape() function, then call the vertex(x, y) for each vertex of the polygon. End the shape with endShape(CLOSE).
beginShape()
vertex(x, y)
endShape(CLOSE)
beginShape(); vertex(x, y); //Coordinate of Vertex 1 vertex(x, y); //Coordinate of Vertex 2 vertex(x, y); //Coordinate of Vertex 3 vertex(x, y); //Coordinate of Vertex 4 vertex(x, y); //Coordinate of Vertex 5 endShape(CLOSE);
arc()
Arcs are part of ellipses (or circles). To draw an arc, we need to specify the start angle and end angle of the portion of the ellipse that we want to draw. In p5js, angles are specified not in degrees but a unit called RADIANS. Angles are also measured in the CLOCKWISE direction.
fill()
stroke()
strokeWeight()
By default the color of closed shapes is set to white and stroke to black.
noStroke()
noFill()
In computer science, variables are placeholder names for actual values
They are used to store data that can be used and manipulated in a program
They allow us to label data with names that make our code more understandable.
colorOfEyes = "red"
mouseX and mouseY - Example
mouseX
mouseY
width and height - Example
width
height
Take 5 minutes and play with the examples above. What do you notice? What do you wonder?
function setup() { createCanvas(400, 400); } function draw() { background(mouseX); fill(255); ellipse(mouseX, mouseY, 50, 50); }
function setup() { //change this value and see what happens to the ellipses createCanvas(600, 120); } function draw() { background(220); fill(255); //place the ellipse by dividing //the built-in variables width and height by 2 ellipse(width / 2, height / 2, 20, 20); noFill(); //get rid of the fill //place this ellipse by dividing //the width and height of the canvas //without using variables ellipse(300, 60, 40, 40); }
The mouseX variable holds the value of the x-coordinate of the current mouse location.
The mouseY variable holds the value of the y-coordinate of the current mouse location.
The values of these two variables will update in real-time as you move your mouse over the canvas.
Based on the code below, what do you think will happen in the sketch?
function setup(){ createCanvas(400,400); } function draw(){ background(mouseX); ellipse(200,200,mouseY); }
To display the mouse location on the canvas, you can add the following line after our background inside the draw() function:
text(mouseX + ',' + mouseY, 20,20);
This text function is receiving the values from mouseX and mouseY and showing them on the canvas at upper-left corner.
Remix the following example to do one or more of the following:
These built-in variables automatically hold the values for the width and height of our canvas. We can use them to place shapes on the canvas.
function setup(){ createCanvas(600,120); } function draw(){ background(220); fill(255); ellipse(width/2, height/2, 20, 20); }
rectMode()
There's a function called rectMode() that allows us to move the rect from it’s center.
The two options for the rectMode() are:
rectMode(
rectMode(CENTER); rectMode(CORNER);
rectMode(CENTER);
rectMode(CORNER);
Play with it here.
In our drawing exercises we placed rectangles based on their upper left corner. If we want a rectangle to be drawn in the center of the screen we might try this, but it’ll place it using the upper left corner.
Go to this sketch and do the following:
function setup() { createCanvas(600, 120); } function draw() { background(180); ellipse(120, 60, 60, 60); ellipse(200, 60, 60, 60); ellipse(280, 60, 60, 60); ellipse(360, 60, 60, 60); ellipse(440, 60, 60, 60); }
What happened when you tried to shift your ellipses 40 pixels down? You might have added “40” to the y value of each ellipse and manually changed the number “60” to “100” five times.
x
y
width/2
height/2
xPosition-30
xPosition
yPosition
color(v1, v2, v3, [alpha]);
myColor= color(81, 100, 200);
fill(myColor);
random()
The random() function picks a random numerical value from the range of values that you put inside the parentheses.
For example, random(5,10) will pick a random value between 5 and 10.
random(5,10)
If you only place one number in the parentheses, the default range starts with 0. In this case random(255) is a random value between 0 and 255.
random(255)
console.log()
var x; function setup() { createCanvas(400, 400); x = random(0,400); } function draw() { console.log(x) }
Using variables and random program a sketch that has the following: